package com.shenhua.linechart;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.Rect;
import android.os.Handler;
import android.util.AttributeSet;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by hbh on 2017/4/10.
 */

public class WaveChart extends SurfaceView implements SurfaceHolder.Callback {


    public WaveChart(Context context) {
        super(context);
    }

    public WaveChart(Context context, AttributeSet attrs) {
        super(context, attrs);
        setZOrderOnTop(true);
        sfh = this.getHolder();
        sfh.addCallback(this);
        sfh.setFormat(PixelFormat.TRANSLUCENT);
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
       //init(sfh);

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                init(sfh);
            }
        },50);
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {

    }
    public void init(SurfaceHolder holder){

        drawBack(holder);

        paint = new Paint();
        paint.setColor(Color.GREEN);
        paint.setStrokeWidth(3);

        cx = X_OFFSET;
        if(task != null){
            task.cancel();
        }
        task = new TimerTask() {

            @Override
            public void run() {
                try {

                int cy =  centerY - (int)(100 * Math.sin((cx - 5) * 2 * Math.PI / 150));
                Canvas canvas = sfh.lockCanvas(new Rect(cx, cy - 2, cx+2, cy + 2));
                canvas.drawPoint(cx, cy, paint);
                cx ++;
                if(cx > WIDTH){
                    task.cancel();
                    task = null;
                }
                sfh.unlockCanvasAndPost(canvas);

                }catch (Exception e){
                   // e.printStackTrace();
                    Log.e("hbh","错误啦");
                }
            }
        };
        timer.schedule(task, 0, 30);
    }

    private SurfaceHolder sfh;

    private Paint paint;
    final int HEIGHT = 320;
    final int WIDTH = 320;
    final int X_OFFSET = 5;
    private int cx = X_OFFSET;
    //实际的Y轴的位置
    int centerY = HEIGHT / 2;
    Timer timer = new Timer();
    TimerTask task = null;

    private  void drawBack(SurfaceHolder holder){
        Canvas canvas = holder.lockCanvas();
        //绘制白色背景
        canvas.drawColor(Color.WHITE);
        Paint p = new Paint();
        p.setColor(Color.BLACK);
        p.setStrokeWidth(2);
        //绘制坐标轴
        canvas.drawLine(X_OFFSET, centerY, WIDTH, centerY, p);
        canvas.drawLine(X_OFFSET, 40, X_OFFSET, HEIGHT, p);
        holder.unlockCanvasAndPost(canvas);
        holder.lockCanvas(new Rect(0, 0, 0, 0));
        holder.unlockCanvasAndPost(canvas);
    }

    public void close(){
        if(task!=null){
            task.cancel();
            task=null;
        }
        if(timer!=null){
            timer.cancel();
            timer=null;
        }
    }
}
